<template>
  <div>
    <!-- 确保ID与你的图表对应 -->
    <div id="echartszhongfooter" class="charts"></div>
  </div>
</template>

<script>
import * as echarts from "echarts"; // 确保已经安装了echarts并正确导入

export default {
  mounted() {
    this.initEcharts();
    window.addEventListener("resize", this.handleWindowResize);
  },
  beforeDestroy() {
    window.removeEventListener("resize", this.handleWindowResize);
  },
  methods: {
    initEcharts() {
      const myChart = echarts.init(
        document.getElementById("echartszhongfooter")
      );

      // 基于准备好的dom，初始化echarts实例

      const option = {
        tooltip: {
          trigger: "axis",
        },

        grid: {
          left: "3%",
          right: "5%",
          top: "8%",
          bottom: "5%",
          containLabel: true,
        },
        color: ["#a4d8cc", "#25f3e6"],
        toolbox: {
          show: false,
          feature: {
            mark: { show: true },
            dataView: { show: true, readOnly: false },
            magicType: { show: true, type: ["line", "bar", "stack", "tiled"] },
            restore: { show: true },
            saveAsImage: { show: true },
          },
        },

        calculable: true,
        xAxis: [
          {
            type: "category",
            axisTick: { show: false },
            boundaryGap: false,
            axisLabel: {
              show: true,
              showMaxLabel: true,
              showMinLabel: true,
              interval: 0,
              textStyle: {
                color: "rgba(255,255,255,.6)",
                fontSize: "12",
              },
              lineStyle: {
                color: "rgba(255,255,255,.1)",
              },

              //   rotate:50,
              formatter: function (params) {
                var newParamsName = ""; // 最终拼接成的字符串
                var paramsNameNumber = params.length; // 实际标签的个数
                var provideNumber = 4; // 每行能显示的字的个数
                var rowNumber = Math.ceil(paramsNameNumber / provideNumber); // 换行的话，需要显示几行，向上取整
                /**
                 * 判断标签的个数是否大于规定的个数， 如果大于，则进行换行处理 如果不大于，即等于或小于，就返回原标签
                 */
                // 条件等同于rowNumber>1
                if (paramsNameNumber > provideNumber) {
                  /** 循环每一行,p表示行 */
                  var tempStr = "";
                  tempStr = params.substring(0, 4);
                  newParamsName = tempStr + "..."; // 最终拼成的字符串
                } else {
                  // 将旧标签的值赋给新标签
                  newParamsName = params;
                }
                //将最终的字符串返回
                return newParamsName;
              },
            },
            data: [
              "星期一",
              "星期二",
              "星期三 ",
              "星期四",
              "星期五",
              "星期六",
              "星期天",
            ],
          },
        ],
        yAxis: {
          min: 300,
          type: "value",
          axisLabel: {
            textStyle: {
              color: "#ccc",
              fontSize: "12",
            },
          },
          axisLine: {
            lineStyle: {
              color: "rgba(160,160,160,0.2)",
            },
          },
          splitLine: {
            lineStyle: {
              color: "rgba(160,160,160,0.2)",
            },
          },
        },

        series: [
          {
            // name:'简易程序案件数',
            lineStyle: {
              color: "#72b0f9",
            },

            type: "line",
            areaStyle: {
              normal: {
                type: "default",
                color: new echarts.graphic.LinearGradient(
                  0,
                  0,
                  0,
                  0.8,
                  [
                    {
                      offset: 0,
                      color: "rgba(129,197,255,.6)",
                    },
                    {
                      offset: 1,
                      color: "rgba(129,197,255,.0)",
                    },
                  ],
                  false
                ),
              },
            },
            smooth: true,
            itemStyle: {
              normal: { areaStyle: { type: "default" } },
            },
            data: [580, 490, 700, 450, 550, 660, 540],
          },
        ],
      };

      // 使用刚指定的配置项和数据显示图表。

      myChart.setOption(option);
    },

    handleWindowResize() {
      this.$nextTick(() => {
        this.myCharts.forEach((chart) => chart.resize());
      });
    },
  },
  data() {
    return {
      myCharts: [], // 用于存储所有echarts实例，以便在窗口调整大小时重新调整图表
    };
  },
};
</script>
